package com.zr.command.controller;

import com.zr.command.domain.EimCommandSchedule;
import com.zr.command.service.EimCommandScheduleGroupLeaderService;
import com.zr.command.service.IEimCommandScheduleService;
import com.zr.common.core.controller.BaseController;
import com.zr.common.core.domain.AjaxResult;
import com.zr.common.core.page.TableDataInfo;
import com.zr.system.service.ISysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/leader/schedule")
public class EimCommandScheduleGroupLeaderController extends BaseController {

    @Resource
    private EimCommandScheduleGroupLeaderService eimCommandScheduleGroupLeaderService;

    @Resource
    private IEimCommandScheduleService eimCommandScheduleService;

    /**
     * 获取组员全部信息
     * 需求模块：APP组长任务分配
     */
    @GetMapping("/memberList")
    public TableDataInfo memberList()
    {
        EimCommandSchedule eimCommandSchedule=new EimCommandSchedule();

        List<EimCommandSchedule> list = eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule);
        return getDataTable(list);
    }

    /**
     * 组长分配任务后，修改任务信息
     * 需求模块：APP组长任务分配
     */
    @PostMapping("/scheduleEdit")
    public AjaxResult scheduleEdit(EimCommandSchedule eimCommandSchedule)
    {
        return toAjax(eimCommandScheduleService.editEimCommandSchedule(eimCommandSchedule));
    }

    /**
     * 获取需求大厅任务
     * 需求模块：APP
     */
    @GetMapping("/leaderList")
    public TableDataInfo leaderList(String status,Long groupId,String executor,String isGroupleader)
    {
        EimCommandSchedule eimCommandSchedule=new EimCommandSchedule();
        eimCommandSchedule.setGroupId(groupId);
        List<EimCommandSchedule> list=new ArrayList<>();
        //如果是组长，并且status不是全部，则获取当前status的任务列表,否则获取全部任务数据
        if(isGroupleader!=null && isGroupleader.equals("Y")){
            if(!status.equals("undefined")&&!status.equals("0")) {
                eimCommandSchedule.setStatus(status);
            }
            list=eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule);
        }
        else {
            //如果是组员，status为”进行中“或”已完成“，则查询当前登录人的任务列表
            if(status.equals("2")||status.equals("3")){
                eimCommandSchedule.setStatus(status);
                eimCommandSchedule.setExecutor(executor);
                list=eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule);
                //如果是组员，status为全部
            }else if(status.equals("undefined")||status.equals("0")){
                //查询该组员进行中的全部任务
                eimCommandSchedule.setExecutor(executor);
                eimCommandSchedule.setStatus("2");
                list=eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule);
                //查询该组员已完成的全部任务
                eimCommandSchedule.setStatus("3");
                list.addAll(eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule));
                //查询组员所在组织，未接受的全部任务
                eimCommandSchedule.setExecutor(null);
                eimCommandSchedule.setStatus("1");
                list.addAll(eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule));
                //如果是组员，status为“未接受”，则查询组员所在组织，未接受的全部任务
            }else if(status.equals("1")){
                eimCommandSchedule.setStatus("1");
                list=eimCommandScheduleGroupLeaderService.selectEimCommandScheduleLeaderList(eimCommandSchedule);
            }
        }
        return getDataTable(list);
    }

    /**
     * 获取单个组员任务信息
     * 需求模块：APP组长任务分配
     */
    @PostMapping("/scheduleOfMember")
    public TableDataInfo scheduleOfMember(EimCommandSchedule eimCommandSchedule){
        List<EimCommandSchedule> list = eimCommandScheduleGroupLeaderService.selectEimCommandscheduleOfMember(eimCommandSchedule);
        return getDataTable(list);
    }

    /**
     * 获取组员任务数量
     * 需求模块：APP组长任务分配
     */
    @PostMapping("/scheduleCount")
    public AjaxResult scheduleCountList(EimCommandSchedule eimCommandSchedule){
        List<HashMap<String,Integer>> scheduleCountList=eimCommandScheduleGroupLeaderService.scheduleCountList(eimCommandSchedule);
        return AjaxResult.success(scheduleCountList);
    }

}